﻿using System;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Wizard.Controllers
{
    [Authenticate]
    public sealed class AddressController : ExtController
    {
        [HttpGet, RestoreState]
        public ActionResult Index()
        {
            var m = Svc.ProcessOperation("GetCustomer", new
            {
                EntityID = JDM.Int32(Svc.Customer.EntityID)
            }, new
            {
                DisclosureLevel = "Entire"
            }).Addresses;
            return View(m);
        }

        [HttpPost, PersistState]
        public ActionResult Create(int countryID,
            int regionID,
            string city,
            string address1,
            string address2,
            string contactName,
            string postalCode,
            string phoneNumber,
            string faxNumber,
            string email)
        {
            try
            {
                Svc.ProcessOperation("CreateCustomerAddress", new
                {
                    HolderID = Svc.Customer.EntityID,
                    CountryID = countryID,
                    RegionID = regionID,
                    City = city,
                    Address1 = address1,
                    Address2 = address2,
                    ContactName = contactName,
                    PostalCode = postalCode,
                    PhoneNumber = phoneNumber,
                    FaxNumber = faxNumber,
                    Email = email,
                    Flags = "Active"
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Index").Localized());
        }
    }
}
